package com.huahai.controller;

import com.huahai.common.BaseResponse;
import com.huahai.exception.ErrorCode;
import com.huahai.pojo.dto.liked.LikedAddDTO;
import com.huahai.pojo.entity.User;
import com.huahai.service.LikedService;
import com.huahai.service.UserService;
import com.huahai.util.ResultUtils;
import com.huahai.util.ThrowUtils;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * 点赞控制器
 *
 * @author huahai
 */
@Slf4j
@RestController
@RequestMapping("/liked")
public class LikedController {

    @Resource
    private LikedService likedService;

    @Resource
    private UserService userService;

    /**
     * 添加点赞
     * @param likedAddDTO 添加点赞请求参数
     * @param request 请求
     * @return 是否成功
     */
    @PostMapping("/like")
    @ApiOperation("点赞操作")
    public BaseResponse<Boolean> addLiked(@RequestBody LikedAddDTO likedAddDTO, HttpServletRequest request) {
        // 校验参数
        ThrowUtils.throwIf(likedAddDTO == null, ErrorCode.PARAMS_ERROR);
        // 获取登录用户信息
        User loginUser = userService.getLoginUser(request);
        // 调用业务层，进行点赞
        Boolean result = likedService.addLiked(likedAddDTO, loginUser);
        return ResultUtils.success(result);
    }
}
